// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "bookName": {
    "rules": [
      {
        "required": true,
        "errorMessage": "病名不能为空"
      },
      {
        "format": "string",
        "errorMessage": "病名不能为空"
      }
    ],
    "title": "病名",
    "label": "病名"
  },
  "author": {
    "rules": [
      {
        "required": true,
        "errorMessage": "医生不能为空"
      },
      {
        "format": "string",
        "errorMessage": "医生不能为空"
      }
    ],
    "title": "医生",
    "label": "医生"
  },
  "publisher": {
    "rules": [
      {
        "required": true,
        "errorMessage": "{title}不能为空"
      },
      {
        "format": "string",
        "errorMessage": "{title}不能为空"
      }
    ],
    "title": "科室",
    "label": "科室"
  },
  "ISBN": {
    "rules": [
      {
        "required": true,
        "errorMessage": "{title}不能为空"
      },
      {
        "format": "string",
        "errorMessage": "{title}不能为空"
      }
    ],
    "title": "成功率",
    "label": "成功率"
  },
  "classify": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "range": [
          {
            "text": "小儿麻痹",
            "value": "儿科"
          },
          {
            "text": "牙疼",
            "value": "牙科"
          },
          {
            "text": "青光眼",
            "value": "眼科"
          },
          {
            "text": "中耳炎",
            "value": "耳科"
          },
          {
            "text": "扁桃体炎",
            "value": "咽喉科"
          },
          {
            "text": "分娩",
            "value": "产科"
          },
          {
            "text": "老年痴呆",
            "value": "精神科"
          }
        ]
      }
    ],
    "title": "分类",
    "label": "分类"
  },
  "coverImage": {
    "rules": [
      {
        "format": "file"
      }
    ],
    "title": "封面",
    "label": "封面"
  },
  "price": {
    "rules": [
      {
        "required": true,
        "errorMessage": "{title}不能为空"
      },
      {
        "format": "int"
      },
      {
        "minimum": 0,
        "errorMessage": "{title}不能小于 {minimum}"
      }
    ],
    "title": "单价",
    "label": "单价"
  },
  "cheapPrice": {
    "rules": [
      {
        "format": "int",
        "errorMessage": "{title}不能小于{minimum}"
      },
      {
        "minimum": 0,
        "errorMessage": "{title}不能小于{minimum}"
      }
    ],
    "title": "折扣价",
    "label": "折扣价"
  },
  "restCount": {
    "rules": [
      {
        "required": true,
        "errorMessage": "{title}不能为空"
      },
      {
        "format": "int"
      },
      {
        "minimum": 0,
        "errorMessage": "{title}不能小于 {minimum}"
      }
    ],
    "title": "余量",
    "label": "余量"
  },
  "brief": {
    "rules": [
      {
        "format": "string",
        "errorMessage": "{title}不能大于{maxLength}字"
      },
      {
        "maxLength": 1024,
        "errorMessage": "{title}不能大于{maxLength}字"
      }
    ],
    "title": "简介",
    "label": "简介"
  }
}

const enumConverter = {
  "classify_valuetotext": {
    "儿科": "小儿麻痹",
    "牙科": "牙疼",
    "眼科": "青光眼",
    "耳科": "中耳炎",
    "咽喉科": "扁桃体炎",
    "产科": "分娩",
    "精神科": "老年痴呆"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
